$(document).ready(function(){

	$(window).resize(function(){
		setBackground();
		//$(".float-div").css(".")
		var nameWidthValue = parseInt($(".name1").css("width")) + (document.documentElement.clientWidth*0.45) - 54;
		$(".project").css("left",(nameWidthValue)+"px");
	});
	setBackground();
	setHeight();
	var newsListLength,currentPager,totalPage,dataId;
	var clickCount = 1;
	$.ajax({
		url:"http://www.leveldisplay.com/getNews.php",
		method:"post",
		data:"type=1",
		success:function(e){
			var caseList = JSON.parse(e);
			var caseListHtml = "";
			newsListLength = caseList.length;
			for(var i = 0;i<caseList.length;i++){
				if(i == 0){
					caseListHtml+= '<li class = "first" data-caseId = '+caseList[i].related_id+'><div class = "title"><p class = "timer">'+caseList[i].news_date+'</p><p>'+caseList[i].title+'</p></div><img src="/news_image/'+caseList[i].news_image+'"><div class = "li-border"></div></li>';	
				}else{
					caseListHtml+= '<li data-caseId = '+caseList[i].related_id+'><div class = "title"><p class = "timer">'+caseList[i].news_date+'</p><p>'+caseList[i].title+'</p></div><img src="/news_image/'+caseList[i].news_image+'"><div class = "li-border"></div></li>'
				}
			}
			$("#news-list").html(caseListHtml);
			currentPager = 1;
			totalPage = Math.ceil(newsListLength/3);
			$(".next-btn").click(function(){		
				$(".pre-btn").css("display","block");
				$(".news-module ul").animate({left:-719*currentPager+"px"},"slow");
				currentPager = currentPager + 1;
				if(currentPager == totalPage){
					$(".next-btn").css("display","none");
				}
			});
			$(".pre-btn").click(function(){
				$(".next-btn").css("display","block");
				currentPager = currentPager - 1;
				$(".news-module ul").animate({left:(-(currentPager-1)*719)+"px"},"slow");
				if(currentPager == 1){
					$(".pre-btn").css("display","none");
				}
			});
			$("#news-list li").click(function(){
				dataId = parseInt($(this).index());
				console.log(dataId);
				$(".news-close").fadeIn("slow");
				$(".news-content").fadeIn("slow");
				$(".news-content img").attr("src","/news_image/"+caseList[dataId].thumbnail);
				$(".news-content .timer").html(caseList[dataId].news_date);
				$(".news-content h1").html(caseList[dataId].title);
				$(".news-content .context").html(caseList[dataId].description);
				$(".news-content a").attr("data-caseid",caseList[dataId].related_id);
			});
		}
	});
	$(".weixin-img").mouseenter(function(e){
		$(".code-for-weixin").show();
	});
	$(".weixin-img").mouseleave(function(e){
		$(".code-for-weixin").hide();
	});
	//设置preject标签左偏移
	var nameWidthValue = parseInt($(".name1").css("width")) + (document.documentElement.clientWidth*0.45) - 54;
	$(".project").css("left",(nameWidthValue)+"px");

	var canMoveMouseFlag = true;
	$(".for-left-nav").mouseenter(function(e){
		if(canMoveMouseFlag){
			canMoveMouseFlag = false;
			$(".nav-top").animate({left: '266px'}, "slow");
			$(".nav-bottom").animate({left: '266px'}, "slow",function(){
				canMoveMouseFlag = true;
			});
		}
		$(".menu a").css("background","url(res/menu2.png) no-repeat");
		$(".menu a").css("background-position","right center");
		$(".level-pager").css("display","none");
	});
	$(".left-nav").mouseleave(function(e){
		if(canMoveMouseFlag){
			canMoveMouseFlag = false;
			$(".nav-top").animate({left: '158px'}, "slow");
			$(".nav-bottom").animate({left: '-7px'}, "slow",function(){
				canMoveMouseFlag = true;
				$(".level-pager").css("display","block");
			});	
		}
	
		$(".menu a").css("background","url(res/menu.png) no-repeat");
		$(".menu a").css("background-position","right center");
	});
	
	
	var preDom;
	$(".news-list").delegate("li","click",function(){
		if(preDom != null){
			preDom.removeClass("selected");
		}
		$(this).children(".li-border").addClass("selected");
		preDom = $(this).children(".li-border");
		$(".news-close").fadeIn("slow");
		$(".news-content").fadeIn("slow");
	});
	//关闭浮动窗口事件
	$(".news-close").click(function(){
		$(".news-close").fadeOut("slow");
		$(".news-content").fadeOut("slow");
	});
	//top导航点击事件
	$(".level-case").click(function(){
		if($(".level-news").hasClass("selected")){
			$(".level-news").removeClass("selected");
			$(".level-news .english-text").removeClass("p-selected");
			$(".level-news .chinese-text").removeClass("p-selected");
		}
		$(".level-case").addClass("selected");
		$(".level-case .english-text").addClass("p-selected");
		$(".level-case .chinese-text").addClass("p-selected");
		$.ajax({
		url:"http://www.leveldisplay.com/getNews.php",
		method:"post",
		data:"type=1",
		success:function(e){
			var caseList = JSON.parse(e);
			var caseListHtml = "";
			newsListLength = caseList.length;
			for(var i = 0;i<caseList.length;i++){
				if(i == 0){
					caseListHtml+= '<li class = "first" data-caseId = '+caseList[i].related_id+'><div class = "title"><p class = "timer">'+caseList[i].news_date+'</p><p>'+caseList[i].title+'</p></div><img src="/news_image/'+caseList[i].news_image+'"><div class = "li-border"></div></li>';	
				}else{
					caseListHtml+= '<li data-caseId = '+caseList[i].related_id+'><div class = "title"><p class = "timer">'+caseList[i].news_date+'</p><p>'+caseList[i].title+'</p></div><img src="/news_image/'+caseList[i].news_image+'"><div class = "li-border"></div></li>'
				}
			}
			$("#news-list").html(caseListHtml);
			currentPager = 1;
			totalPage = Math.ceil(newsListLength/3);
			$(".next-btn").click(function(){		
				$(".pre-btn").css("display","block");
				$(".news-module ul").animate({left:-719*currentPager+"px"},"slow");
				currentPager = currentPager + 1;
				if(currentPager == totalPage){
					$(".next-btn").css("display","none");
				}
			});
			$(".pre-btn").click(function(){
				$(".next-btn").css("display","block");
				currentPager = currentPager - 1;
				$(".news-module ul").animate({left:(-(currentPager-1)*719)+"px"},"slow");
				if(currentPager == 1){
					$(".pre-btn").css("display","none");
				}
			});
			$("#news-list li").click(function(){
				dataId = parseInt($(this).index());
				console.log(dataId);
				$(".news-close").fadeIn("slow");
				$(".news-content").fadeIn("slow");
				$(".news-content img").attr("src","/news_image/"+caseList[dataId].thumbnail);
				$(".news-content .timer").html(caseList[dataId].news_date);
				$(".news-content h1").html(caseList[dataId].title);
				$(".news-content .context").html(caseList[dataId].description);
				$(".news-content a").attr("data-caseid",caseList[dataId].related_id);
			});
		}
	});
	});
	$(".level-news").click(function(){
		if($(".level-case").hasClass("selected")){
			$(".level-case").removeClass("selected");
			$(".level-case .english-text").removeClass("p-selected");
			$(".level-case .chinese-text").removeClass("p-selected");
		}
		$(".level-news").addClass("selected");
		$(".level-news .english-text").addClass("p-selected");
		$(".level-news .chinese-text").addClass("p-selected");
		$(".level-news")
		$.ajax({
		url:"http://www.leveldisplay.com/getNews.php",
		method:"post",
		data:"type=2",
		success:function(e){
			var caseList = JSON.parse(e);
			console.log(caseList);
			var caseListHtml = "";
			newsListLength = caseList.length;
			for(var i = 0;i<caseList.length;i++){
				if(i == 0){
					caseListHtml+= '<li class = "first" data-caseId = '+caseList[i].related_id+'><div class = "title"><p class = "timer">'+caseList[i].news_date+'</p><p>'+caseList[i].title+'</p></div><img src="/news_image/'+caseList[i].news_image+'"><div class = "li-border"></div></li>';	
				}else{
					caseListHtml+= '<li data-caseId = '+caseList[i].related_id+'><div class = "title"><p class = "timer">'+caseList[i].news_date+'</p><p>'+caseList[i].title+'</p></div><img src="/news_image/'+caseList[i].news_image+'"><div class = "li-border"></div></li>'
				}
			}
			$("#news-list").html(caseListHtml);
			currentPager = 1;
			//console.log(newsListLength);
			totalPage = Math.ceil(newsListLength/3);
			$(".next-btn").click(function(){		
				$(".pre-btn").css("display","block");
				$(".news-module ul").animate({left:-719*currentPager+"px"},"slow");
				currentPager = currentPager + 1;
				if(currentPager == totalPage){
					$(".next-btn").css("display","none");
				}
			});
			$(".pre-btn").click(function(){
				$(".next-btn").css("display","block");
				currentPager = currentPager - 1;
				$(".news-module ul").animate({left:(-(currentPager-1)*719)+"px"},"slow");
				if(currentPager == 1){
					$(".pre-btn").css("display","none");
				}
			});
			$("#news-list li").click(function(){
				console.log($(this));
				dataId = parseInt($(this).index());
				$(".news-close").fadeIn("slow");
				$(".news-content").fadeIn("slow");
				$(".news-content img").attr("src","/news_image/"+caseList[dataId].thumbnail);
				$(".news-content .timer").html(caseList[dataId].news_date);
				$(".news-content h1").html(caseList[dataId].title);
				$(".news-content .context").html(caseList[dataId].description);
				$(".news-content a").attr("data-caseid",caseList[dataId].related_id);
			});
		}
	});
	});
	$(".news-content a").click(function(){
		document.location.href = "http://www.leveldisplay.com/cases.php?id="+$(this).attr("data-caseid");
	});
	//切换背景
	changeBackground();
	//展示信息详情
	//切换背景

	var flag = 0;
	var flagShowP = 0;

});
function setHeight(){
	$("#surprised .slide-01 img").css("height","949px");
	$(".background-img").css("height","949px");
	$(".background-img").css("background","url(res/background.png) repeat");
}
function changeBackground(){
	var picCount1 = 0;
	var picCount2 = 1;
	var i = 0;
	//标签计数
	var j = 1;
	var maxWidth = $(window).width();
	//alert(maxWidth);
	var timer = setInterval(changeBackImg,5);
	var listLeng = $("#surprised li").length;
	var flushFlag = false;
	function changeBackImg(){
		//alert(i);
		i = i + 1;
		if(i>maxWidth){
			if( picCount1 == listLeng){
				picCount1 = 0;
				setVis($("#surprised li:eq("+picCount1+")").find("img"));
				$(".name a:eq("+picCount1+")").css("display","none");
				$(".name-info p:eq("+picCount1+")").css("display","none");
				picCount1 = picCount1 + 1;
				
			}else{
				setVis($("#surprised li:eq("+picCount1+")").find("img"));
				$(".name a:eq("+picCount1+")").css("display","none");
				$(".name-info p:eq("+picCount1+")").css("display","none");
				picCount1 = picCount1 + 1;
			}
			currentImg = picCount1;
			if(picCount2 == listLeng){
				picCount2 = 0;
				setOpa($("#surprised li:eq("+picCount2+")").find("img"));
				$(".name a:eq("+picCount2+")").css("display","block");
				$(".name-info p:eq("+picCount2+")").css("display","block");
				picCount2 = picCount2 + 1;
			}else{
				setOpa($("#surprised li:eq("+picCount2+")").find("img"));
				$(".name a:eq("+picCount2+")").css("display","block");
				$(".name-info p:eq("+picCount2+")").css("display","block");
				picCount2 = picCount2 + 1;
			}
			i = 0;
			if( j == listLeng){
				j = 0;
				var nameWidthValue = parseInt($(".name1").css("width")) + (document.documentElement.clientWidth*0.45) - 54;
				$(".project").css("left",(nameWidthValue)+"px");
				j = j + 1;
			}else{
				var nameWidthValue = parseInt($(".name a:eq("+j+")").css("width")) + (document.documentElement.clientWidth*0.45) - 54;
				$(".project").css("left",(nameWidthValue)+"px");
				j = j + 1;
			}
		}
		$("#progress-bar").css("left",i+"px");
	}
}
//隐藏前一个元素
function setVis(imgDom){
	var opaImg = 1;
		var timerO = setInterval(runO,10);
		function runO(){
			imgDom.css("z-index","-30");
			opaImg = opaImg - 0.05;
			imgDom.css("opacity",opaImg);
			if(imgDom.css("opacity") == 0){
				clearInterval(timerO);
				imgDom.css("display","none");
			}
		}
}
//展示下一个元素
function setOpa(imgDom){
	var opaImg = 0;
		var timerO = setInterval(runO,10);
		function runO(){
			imgDom.css("z-index","-10");
			imgDom.css("display","block");
			opaImg = opaImg + 0.05;
			imgDom.css("opacity",opaImg);
			if(imgDom.css("opacity") == 1){
				clearInterval(timerO);
			}
		}
}

setBackground = function (){
	$("#surprised").width($(window).width());
	$(".footer").height($(window).height()-381);
	if($(window).height() < 520){
		$(".footer").height(123);
	}
}